package com.hdu;

import com.hdu.mockService.service.MockService;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class TestMain {

    public static List<Student> mockStudents = new ArrayList<>();

    static {
        /**
         * mockData
         */
        for (int i = 0; i < 1000; i++) {
            Student student = MockService.mock(Student.class);
            mockStudents.add(student);
        }
    }

    public static void main(String[] args) {
        testParallelConsume();
        mockStudents.forEach(student -> log.info("student : {}", student));
    }

    public static void testParallelConsume() {
        List<Integer> ids = new ArrayList<>();
        for (int i = 1; i <= 1000; i++) {
            ids.add(i);
        }
        ParallelExecuteTemplate.parallelConsume(
                ids,
                50,
                idList -> {
                    for (Integer id : idList) {
                        for (Student mockStudent : mockStudents) {
                            if (mockStudent.getId().equals(id)) {
                                mockStudent.setId(id * 2);
                            }
                        }
                    }
                }
        );
    }

    public static void testParallelGetList() {
        List<Integer> ids = new ArrayList<>();
        for (int i = 1; i <= 1000; i++) {
            ids.add(i);
        }
        List<Student> students = ParallelExecuteTemplate.parallelGetList(
                ids,
                50,
                idList -> {
                    List<Student> studentList = new ArrayList<>();
                    for (Integer id : idList) {
                        for (Student mockStudent : mockStudents) {
                            if (mockStudent.getId().equals(id)) {
                                studentList.add(mockStudent);
                            }
                        }
                    }
                    return studentList;
                }
        );
        int total = students.size();
        log.info("total query size : {}", total);
    }

    public static void testParallelGetMap() {
        List<Integer> ids = new ArrayList<>();
        for (int i = 1; i <= 1000; i++) {
            ids.add(i);
        }
        Map<Integer, List<Student>> id2Student = ParallelExecuteTemplate.parallelGetMap(
                ids,
                50,
                subIdList -> {
                    Map<Integer, List<Student>> id2student = new HashMap<>();
                    for (Integer id : subIdList) {
                        for (Student mockStudent : mockStudents) {
                            if (mockStudent.getId().equals(id)) {
                                id2student.putIfAbsent(
                                        id,
                                        new ArrayList<>()
                                );
                                id2student.get(id).add(mockStudent);
                            }
                        }
                    }
                    return id2student;
                }
        );
        int total = 0;
        for (Integer id : id2Student.keySet()) {
            total += id2Student.get(id).size();
        }
        log.info("total query size : {}", total);
    }
}
